import 'package:base/base.dart';
import 'package:flutter/material.dart';

/// BaseSection
/// use custom Container by cupertino or material
class BaseSection extends BaseStatelessWidget {
  const BaseSection({
    Key? baseKey,
    this.key,
    this.margin,
    this.divider = const BaseSectionDivider(),
    this.children = const [],
    this.header,
    this.footer,
    Map<String, dynamic>? cupertino,
    Map<String, dynamic>? material,
  }) : super(
      key: baseKey,
      cupertino: cupertino ?? const <String, dynamic>{},
      material: material ?? const <String, dynamic>{}
  );

  @override
  final Key? key;
  final EdgeInsets? margin;
  final BaseSectionDivider divider;

  final List<Widget> children;

  final Widget? header;
  final Widget? footer;

  @override
  Widget buildByCupertino(BuildContext context) {
    final Key? key = valueFromCupertino('key', this.key);
    final EdgeInsets? margin = valueFromCupertino('margin', this.margin);
    final BaseSectionDivider divider = valueFromCupertino(
      'divider',
      this.divider,
    );
    final List<Widget> children = valueFromCupertino('children', this.children);
    final Widget? header = valueFromCupertino('header', this.header);
    final Widget? footer = valueFromCupertino('footer', this.footer);
    return _build(key, context, margin, divider, children, header, footer);
  }

  @override
  Widget buildByMaterial(BuildContext context) {
    final Key? key = valueFromMaterial('key', this.key);
    final EdgeInsets? margin = valueFromMaterial('margin', this.margin);
    final BaseSectionDivider divider = valueFromMaterial(
      'divider',
      this.divider,
    );
    final List<Widget> children = valueFromMaterial('children', this.children);
    final Widget? header = valueFromMaterial('header', this.header);
    final Widget? footer = valueFromMaterial('footer', this.footer);
    return _build(key, context, margin, divider, children, header, footer);
  }

  Widget _build(
      Key? key,
      BuildContext context,
      EdgeInsets? margin,
      BaseSectionDivider divider,
      List<Widget> children,
      Widget? header,
      Widget? footer,
      ) {
    final List<Widget> widgets = <Widget>[];
    final BaseThemeData baseTheme = BaseTheme.of(context);
    final Color? color = divider.color ?? baseTheme.sectionDividerColor;

    final UnderlineTabIndicator _divider = UnderlineTabIndicator(
      borderSide: BorderSide(
        width: divider.height,
        color: color ?? Colors.grey, // 提供默认颜色
      ),
      insets: divider.insets,
    );

    // 添加 header（如果存在）
    if (header != null) {
      widgets.add(header);
    }

    // 处理 children
    final int length = children.length;
    for (int i = 0; i < length; i++) {
      Widget child = Container(
        child: children[i],
      );
      if (i != length - 1) {
        child = Container(
          child: child,
          foregroundDecoration: _divider,
        );
      }
      widgets.add(child);
    }

    // 添加 footer（如果存在）
    if (footer != null) {
      widgets.add(footer);
    }

    return Container(
      key: key,
      margin: margin,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: widgets,
      ),
    );
  }
}

class BaseSectionDivider {
  const BaseSectionDivider({
    this.color,
    this.height = .5,
    this.insets = const EdgeInsets.only(left: 10.0),
  });

  final Color? color;
  final double height;
  final EdgeInsetsGeometry insets;
}